// JavaScript Document
var ISEND = true;

var ShareTitle	=	'新春报道 祝福豹到',
	ShareDesc1	=	'2015新春豹到！文字祝福不过瘾，玩转语音才够COOL！',
	ShareDesc2	=	'新春豹到，'+ NickName +'为你送来语音祝福，快来听听吧！',
	ShareImg = 'http://api.weixinzjit.com/zf/img/ico.jpg',
	ShareUrl1 = 'http://api.weixinzjit.com/zf/',
	ShareUrl2 = 'http://api.weixinzjit.com/zf/card.php?vid=',
	ShareDesc	=	ShareDesc1,
	ShareUrl	=ShareUrl1;

var voice = {
    localId: '',
    serverId: ''
};
  
wx.ready(function () {
	//$('#framebox').width($(window).width()).height($(window).width()/640*1008);
	$('#framebox img').css('bottom','0px');
	$('.music_btn').addClass('music_btn_on'); 
	document.getElementById('index_music').play();
	wx.startRecord({
		  cancel: function () {
			alert('用户拒绝授权录音');
		  },
		  success: function (res) {  
		  		setTimeout(function(){ wx.stopRecord({  
					  success: function (res) { },
					  fail: function (res) { }
				});  },100);
		  }
	}); 
	
   $('#recordbtn').bind('touchstart', function(ev){  
	  	ev.preventDefault(); 
	  	ShareDesc = ShareDesc2;
		$('.music_btn').removeClass('music_btn_on');
		document.getElementById('index_music').pause();
		$('#recordbtnsc2').addClass('recordbtnsc2');
		$('#recordbtnsc2').fadeIn();
		$('#touchtxt img').hide();
		$('#touchtxt,#touchtxt .txt_end').fadeIn();
		resetShare();
		wx.startRecord({
		  cancel: function () {
			alert('用户拒绝授权录音');
		  } 
		}); 
	}).bind('touchend',function(){
		wx.stopRecord({
		  success: function (res) {
			$('#recordbtn').fadeOut();
			$('#recordviewbtn , #bottom_btn').fadeIn();
			$('#recordbtnsc2').fadeOut();
			$('#touchtxt,#touchtxt img').fadeOut();
			voice.localId = res.localId;
		  },
		  fail: function (res) {
			$('#recordbtnsc').fadeOut();
			$('#touchtxt,#touchtxt img').hide();
			alert('祝福还不够哦～还请多说几句吧！');
		  }
		}); 
	});
	
	$('#recordviewbtn').bind('touchstart', function(ev){ 
	  	ev.preventDefault();
		if(!ISEND){ return false; }
		ISEND = false;
		if(voice.localId){  
			$('#bottom_btn').fadeOut();
			$('#recordbtnsc').addClass('recordbtnsc');
			$('#recordbtnsc').fadeIn();
			wx.playVoice({
				  localId: voice.localId
			});
		}else{
			alert('请先使录制一段声音');
		}
	});

	wx.onVoicePlayEnd({
		complete: function (res) {
			$('#recordbtnsc').fadeOut();
			$('#recordviewbtn , #bottom_btn').fadeIn();
			$('#touchtxt img').hide();
			ISEND = true;
		}
	});

	$('#save_btn').bind('touchstart', function(ev){ 
	  	ev.preventDefault();
		if (voice.localId == '') {
			alert('请先使录制一段声音'); 
		}else{
			if(voice.serverId.length<1){
				wx.uploadVoice({
					localId: voice.localId,
					success: function (res) {
						//url = 'log.php?d=sid['+res.serverId+']lid['+voice.localId+']';
						//$.get(url,function(d){ });
						voice.serverId = res.serverId;
						ShareUrl	=	ShareUrl2+voice.serverId;
						resetShare();
					}
				});
			}
			
			//$('#sharediv').fadeIn();
			$('#logo').fadeOut();
			$('#cardbg').fadeIn();
			$('#recordbtn,#recordbtnsc,#recordbtnsc2,#recordbtnbg,#recordviewbtn,#bottom_btn').fadeOut();
			playFrame(4); 
			
		}  
	});
	
	resetShare();
});

function resetShare(){
	wx.onMenuShareAppMessage({
	      title: ShareTitle,
	      desc: ShareDesc,
	      link: ShareUrl,
	      imgUrl: ShareImg,
	      trigger: function (res) { /* alert('用户点击发送给朋友'); */ },
	      success: function (res) {  /* alert('已分享'); */
			$('#framebox img').hide();
			$('#music_btn,#logo').fadeIn();
			$('#sharediv').fadeOut();
			$('#logo').hide();
			$('#cardshareBtn,.cardshareTxt').hide();
			$('#cardshareBtn2').fadeIn();
			$('#framebox img.framebox_189').fadeIn();
		},
	      cancel: function (res) { /* alert('已取消'); */ },
	      fail: function (res) { /* alert(JSON.stringify(res)); */ }
	 }); 
	 
	 wx.onMenuShareTimeline({ // 2.2 监听"分享到朋友圈"按钮点击、自定义分享内容及分享结果接口
	      title: ShareDesc, 
	      link: ShareUrl, 
	      imgUrl: ShareImg,
	      trigger: function (res) { /* alert('用户点击分享到朋友圈'); */ },
	      success: function (res) {  /* alert('已分享'); */
			$('#framebox img').hide();
			$('#music_btn,#logo').fadeIn();
			$('#sharediv').fadeOut();
			$('#cardshareBtn,.cardshareTxt').hide();
			$('#cardshareBtn2').fadeIn();
			$('#logo').hide();
			$('#framebox img.framebox_189').fadeIn();
		},
	      cancel: function (res) { /* alert('已取消'); */ },
	      fail: function (res) { /* alert(JSON.stringify(res)); */ }
	 }); 
}